using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Buddy.Coroutines;
using log4net;
using Triton.Common.LogUtilities;

namespace Triton.Bot.Profiles
{
	[ProfileElementName("If")]
	[XmlRoot("If")]
	public class IfTag : ConditionalProfileGroupElement
	{
		[CompilerGenerated]
		private sealed class Class216
		{
			public ProfileElement profileElement_0;

			internal Task method_0()
			{
				return profileElement_0.ProfileTagLogic();
			}
		}

		[Serializable]
		[CompilerGenerated]
		private sealed class Class217
		{
			public static readonly Class217 Instance9 = new Class217();

			public static Func<ProfileElement, Coroutine> Instance9__14_0;

			internal Coroutine method_0(ProfileElement profileElement_0)
			{
				return new Coroutine((Func<Task>)new Class216
				{
					profileElement_0 = profileElement_0
				}.method_0);
			}
		}

		private readonly ILog ilog_0 = Logger.GetLoggerInstanceForType();

		private bool bool_0;

		public override string Name => "If";

		public override string Author => "Bossland GmbH";

		public override string Description => "";

		public override string Version => "1.0.0.0";

		public override bool IsFinished => bool_0;

		public override void Reset()
		{
			base.Reset();
			bool_0 = false;
		}

		public override async Task ProfileTagLogic()
		{
			if (Condition)
			{
				foreach (ProfileElement child in Children)
				{
					child.Reset();
					while (!child.IsFinished)
					{
						await child.ProfileTagLogic();
						await Coroutine.Yield();
					}
				}
			}
			bool_0 = true;
		}

		private async Task method_0()
		{
			Coroutine[] array = Children.Select((ProfileElement profileElement_0) => new Coroutine(() => profileElement_0.ProfileTagLogic())).ToArray();
			try
			{
				while (Condition)
				{
					bool flag = false;
					Coroutine[] array2 = array;
					foreach (Coroutine coroutine in array2)
					{
						if (!coroutine.IsFinished)
						{
							coroutine.Resume();
							flag = true;
						}
					}
					if (!flag)
					{
						break;
					}
				}
			}
			finally
			{
				Coroutine[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					array2[i].Dispose();
				}
			}
		}
	}
}
